<template>
  <div class="app-container">
    <el-form v-show="showSearch" ref="queryForm" :model="queryParams" label-width="68px" size="mini">
      <el-row :gutter="20">
        <el-col :span="4">
          <el-form-item label="项目区域" prop="projectRegion">
            <el-select v-model="queryParams.projectRegions" clearable placeholder="请选择"
                       @keyup.enter.native="handleQuery">
              <el-option v-for="dict in dict.type.cld_project_region" :key="dict.value" :label="dict.label"
                         :value="parseInt(dict.value)"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="项目名称" prop="projectName">
            <el-input v-model="queryParams.projectName" clearable placeholder="请输入"
                      @keyup.enter.native="handleQuery"/>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="职务" prop="projectDuitsName">
            <el-select v-model="queryParams.projectDuitsName" clearable placeholder="请选择"
                       @keyup.enter.native="handleQuery">
              <el-option v-for="dict in dict.type.cld_project_duits" :key="dict.value" :label="dict.label"
                         :value="parseInt(dict.value)"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="审核状态" prop="approveStatus">
            <el-select v-model="queryParams.approveStatus" clearable placeholder="请选择"
                       @keyup.enter.native="handleQuery">
              <el-option v-for="dict in approveStatusOptions" :key="dict.value" :label="dict.label"
                         :value="parseInt(dict.value)"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label-width="10px">
            <el-button icon="el-icon-search" size="mini" type="primary" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button v-hasPermi="['cld:CldReplace:add']" icon="el-icon-plus" plain size="mini" type="primary"
                   @click="handleAdd">新增补卡
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="CldReplaceList" @selection-change="handleSelectionChange" border>
      <el-table-column align="center" label="项目区域" prop="projectRegion">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.cld_project_region" :value="scope.row.projectRegion"/>
        </template>
      </el-table-column>
      <el-table-column align="center" label="项目名称" prop="projectName"/>
      <el-table-column align="center" label="岗位名称" prop="projectPostName">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.cld_project_post" :value="scope.row.projectPostName"/>
        </template>
      </el-table-column>
      <el-table-column align="center" label="姓名" prop="staffName"/>
      <el-table-column align="center" label="补卡类型" prop="replaceType">
        <template slot-scope="scope">
          <span v-if="scope.row.replaceType == '1'">上班卡</span>
          <span v-if="scope.row.replaceType == '2'">下班卡</span>
          <span v-if="scope.row.replaceType == '1,2'">上下班卡</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="补卡日期" prop="replaceTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.replaceTime,'{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="补卡说明" prop="replaceRemark"/>
      <el-table-column align="center" label="提交时间" prop="createTime"/>
      <el-table-column align="center" label="审核状态" prop="approveStatus">
        <template slot-scope="scope">
          <dict-tag :options="approveStatusOptions" :value="scope.row.approveStatus"/>
        </template>
      </el-table-column>
      <el-table-column align="center" label="审核人" prop="approveName"/>
      <el-table-column align="center" label="审批时间" prop="approveTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.approveTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" class-name="small-padding fixed-width" label="操作">
        <template slot-scope="scope">
          <el-button v-if="scope.row.approveStatus == 2" v-hasPermi="['cld:CldReplace:edit']" size="mini" type="text"
                     @click="handleUpdate(scope.row)">修改
          </el-button>
          <el-button v-if="scope.row.approveStatus == 1" v-hasPermi="['cld:CldReplace:audit']" size="mini" type="text"
                     @click="handleAuditOptions(scope.row, 3)">通过
          </el-button>
          <el-button v-if="scope.row.approveStatus == 1" v-hasPermi="['cld:CldReplace:audit']" size="mini" type="text"
                     @click="handleAuditOptions(scope.row, 2)">驳回
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :limit.sync="queryParams.pageSize" :page.sync="queryParams.pageNum" :total="total"
                @pagination="getList"/>

    <!-- 添加或修改补卡管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" append-to-body width="50%">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px" size="mini">
        <el-row>
          <el-col :span="8">
            <el-form-item label="项目名称" prop="projectId">
              <el-select v-model="form.projectId" placeholder="请选择" @change="handleProjectChange">
                <el-option v-for="item in projectOptions" :key="item.projectId" :label="item.projectName"
                           :value="item.projectId"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="补卡日期" prop="replaceTime">
              <el-date-picker v-model="form.replaceTime" clearable placeholder="请选择补卡时间" type="date"
                              value-format="yyyy-MM-dd">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="补卡类型" prop="replaceTypes">
              <el-select v-model="form.replaceTypes" multiple placeholder="请选择">
                <el-option label="上班卡" value="1"></el-option>
                <el-option label="下班卡" value="2"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="补卡说明" prop="replaceRemark">
              <el-input v-model="form.replaceRemark" :rows="2" placeholder="请输入" type="textarea"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button :disabled="!form.projectId" icon="el-icon-plus" plain size="mini" type="primary"
                       @click="handleAddStaff">添加员工
            </el-button>
          </el-col>
        </el-row>
        <el-table :data="form.staffList" border>
          <el-table-column align="center" label="岗位名称" prop="staffName"/>
          <el-table-column align="center" label="姓名" prop="staffName"/>
          <el-table-column align="center" label="职务" prop="projectDuitsName">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.cld_project_duits" :value="scope.row.projectDuitsName"/>
            </template>
          </el-table-column>
          <el-table-column align="center" label="性别" prop="staffSex"/>
          <el-table-column align="center" label="年龄">
            <template slot-scope="scope">
              {{ calculateAge(scope.row.staffIdcard) }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="联系电话" prop="staffSex"/>
          <el-table-column align="center" class-name="small-padding fixed-width" label="操作">
            <template slot-scope="scope">
              <el-button size="mini" type="text" @click="handleDeleteStaff(scope.row)">移除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


    <!-- 选择人员 -->
    <el-dialog :close-on-click-modal="false" :destroy-on-close="true" :visible.sync="isStaffOpen" append-to-body title="选择人员"
               width="70%">
      <SelectedStaff v-if="isStaffOpen" :isMultiple="true" :isProject="false" :params="{ notStaffIds: notStaffIds.join(','), staffStatus: 2, projectId: form.projectId }"
                     @multipleInput="handleStaffSelected">
      </SelectedStaff>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitSelectedStaff">确 定</el-button>
        <el-button @click="isStaffOpen = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {addCldReplace, auditCldReplace, getCldReplace, listCldReplace, updateCldReplace} from "@/api/cld/CldReplace";
import {getList} from '@/api/cld/project'
import {approveStatusOptions} from '@/assets/staticDict'
import SelectedStaff from '@/views/cld/staff/selectedStaff';

export default {
  name: "CldReplace",
  components: {SelectedStaff},
  dicts: ['cld_project_post', 'cld_project_region', 'cld_project_duits'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 补卡管理表格数据
      CldReplaceList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        projectRegions: null,
        projectName: null,
        projectDuitsName: null,
        approveStatus: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        projectId: [
          {required: true, message: "请选择项目", trigger: "change"}
        ],
        replaceTime: [
          {required: true, message: "补卡日期不能为空", trigger: "blur"}
        ],
        replaceTypes: [
          {required: true, message: "补卡类型不能为空", trigger: "change"}
        ]
      },
      // 项目选择
      projectOptions: [],
      // 审核状态
      approveStatusOptions: approveStatusOptions,
      // 选择员工弹框
      isStaffOpen: false,
      // 已选择员工
      notStaffIds: [],
      // 临时存放选择员工
      temporaryList: [],
    };
  },
  created() {
    this.getList();
    // 查询项目
    this.handleGetProjectList();
  },
  methods: {
    /** 查询补卡管理列表 */
    getList() {
      this.loading = true;
      listCldReplace(this.queryParams).then(response => {
        this.CldReplaceList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 查询项目
    handleGetProjectList() {
      getList({
        projectStatus: 2
      }).then(res => {
        this.projectOptions = res.data;
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        replaceId: null,
        projectId: null,
        staffId: null,
        replaceTime: null,
        replaceRemark: null,
        approveStatus: null,
        approveId: null,
        approveName: null,
        approveTime: null,
        resultReason: null,
        staffList: [],
        replaceTypes: []
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加补卡管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const replaceId = row.replaceId || this.ids
      getCldReplace(replaceId).then(response => {
        this.form = response.data;
        this.form.replaceTypes = this.form.replaceType.split(',');
        this.open = true;
        this.title = "修改补卡管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.replaceType = this.form.replaceTypes.join(',');
          if (this.form.replaceId != null) {
            updateCldReplace(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCldReplace(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    // 审核
    handleAuditOptions(row, type) {
      this.form = {
        approveStatus: type,
        replaceId: row.replaceId
      }
      if (type == 2) {
        this.$prompt('', '驳回原因', {
          center: true,
          inputType: 'textarea',
          confirmButtonText: '确定',
          cancelButtonText: '取消',
        }).then(({value}) => {
          row.resultReason = value;
          this.handleAudit();
        })
      } else {
        this.handleAudit();
      }
    },
    // 审核提交
    handleAudit() {
      auditCldReplace(this.form).then(res => {
        this.$modal.msgSuccess("操作成功");
        this.getList();
      })
    },
    // 添加员工
    handleAddStaff() {
      // 筛选已选择人员
      this.notStaffIds = this.form.staffList.map(item => item.staffId);
      this.isStaffOpen = true;
    },
    // 员工选择监听
    handleStaffSelected(e) {
      this.temporaryList = e;
    },
    // 员工确认选择
    submitSelectedStaff() {
      this.temporaryList.forEach(item => {
        this.form.staffList.push(item)
      })
      this.isStaffOpen = false;
    },
    // 移除员工
    handleDeleteStaff(row) {
      const index = this.form.staffList.indexOf(row);
      if (index > -1) {
        this.form.staffList.splice(index, 1);
      }
    },
  }
};
</script>
<style scoped>
.el-select {
  width: 100%;
}

.el-date-editor.el-input,
.el-date-editor.el-input__inner {
  width: 100%;
}
</style>
